import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import AppLayout from '@/components/layout/AppLayout.vue'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: AppLayout,
    redirect: '/home/workspace',
    children: [
      {
        path: 'workspace',
        name: 'workspace',
        component: () => import('../views/Home.vue')
      }]
  },
  {
    path: '/collect',
    component: AppLayout,
    children: [
      {
        path: 'extract',
        name: 'extract',
        component: () => import('../views/Collect/Extract.vue')
      },
      {
        path: 'load',
        name: 'load',
        component: () => import('../views/Collect/Load.vue')
      },
      {
        path: 'translate',
        name: 'translate',
        component: () => import('../views/Collect/Translate.vue')
      }
    ]
  },
  {
    path: '/analysis',
    component: AppLayout,
    children: [
      {
        path: 'search',
        name: 'search',
        component: () => import('../views/Analysis/Search.vue')
      },
      {
        path: 'pivot',
        name: 'pivot',
        component: () => import('../views/Analysis/Graph/Get.vue')
      },
      {
        path: 'graph',
        name: 'graph',
        children: [
          {
            path: '',
            name: 'list',
            component: () => import('../views/Analysis/Graph/List.vue')
          },
          {
            path: 'get',
            name: 'get',
            component: () => import('../views/Analysis/Graph/Get.vue')
          }
        ]
      },
      {
        path: 'panel',
        name: 'panel',
        component: () => import('../views/Analysis//Panel.vue')
      },
      {
        path: 'alert',
        name: 'alert',
        component: () => import('../views/Analysis/Alert.vue')
      },
      {
        path: 'overview',
        name: 'overview',
        component: () => import('../views/Analysis/Overview.vue')
      },
      {
        path: 'map',
        name: 'Map',
        component: () => import('../views/Analysis/Map.vue')
      },
      {
        path: 'map2',
        name: 'Map2',
        component: () => import('../views/Analysis/Map2.vue')
      }
    ]
  },
  {
    path: '/devops',
    component: AppLayout,
    children: [
      {
        path: 'aiops',
        name: 'Aiops',
        component: () => import('../views/Devops/Aiops.vue')
      },
      {
        path: 'relate',
        name: 'Relate',
        component: () => import('../views/Devops/Relate.vue')
      }
    ]
  },
  {
    path: '/system',
    name: 'system',
    component: AppLayout,
    children: [
      { path: 'basic', component: () => import('../views/System/Basic.vue') },  // 基础配置
      { path: 'safe', component: () => import('../views/System/Safe.vue') },   // 安全配置
      { path: 'dict', component: () => import('../views/System/Enum.vue') },   // 字典配置
    ]
  },
  {
    path: '/user',
    name: 'user',
    component: AppLayout,
    children: [
      { path: 'user', name: 'User', component: () => import('../views/User/User.vue'), meta: { waterMark: true } },
      { path: 'role', name: 'Role', component: () => import('../views/User/Role.vue') }
    ]
  },
  {
    path: '/owner',
    name: 'owner',
    component: AppLayout,
    children: [
      { path: 'settings', name: 'ownerSettings', component: () => import('../views/Owner/Owner.vue') }
    ]
  },
  {
    path: '/tool',
    name: 'tool',
    children: [
      { path: 'lock', name: 'Lock', component: () => import('../views/Tool/LockScreen/LockScreen.vue') }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()

  const isAuthenticated = userStore.token
  if (to.name !== 'Login' && !isAuthenticated) {
    next({ name: 'Login' })
  }
  else if (to.name === 'Login' && isAuthenticated) {
    // 如果已登录，跳转到首页
    next({ name: 'Home' })
  }
  else {
    next()
  }
})

export default router
